package com.qj.platform.pay.core.service;

import com.qj.base.commons.domain.SearchObject;
import com.qj.base.commons.exception.CommonException;
import com.qj.base.commons.log.LoggerFactory;
import com.qj.base.commons.utils.CodeGenerator;
import com.qj.platform.pay.core.dao.MerchantDao;
import com.qj.platform.pay.core.dao.OrderDao;
import com.qj.platform.pay.core.domain.Order;
import com.qj.platform.pay.mssm.domain.MSNotify;
import com.qj.platform.pay.mssm.domain.Merchant;
import com.qj.platform.pay.mssm.domain.PaySMBody;
import com.qj.platform.pay.mssm.service.MSSMPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhijie on 2016/12/14.
 */
@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MerchantDao merchantDao;

    public Order getById(String id) {
        return orderDao.getById(id);
    }

    public String insert(Order order) {
        String id = orderDao.insert(order);
        return id;
    }

    public String paySM(String merchantId,long totalAmount){
        PaySMBody body=new PaySMBody();
        Merchant merchant=merchantDao.getById(merchantId);
        if(merchant==null){
            throw new CommonException("商户已失效");
        }
        if(totalAmount<=0){
            throw new CommonException("支付金额不正确");
        }
        body.setTotalAmount(totalAmount/100.00);
        body.setMerchantCode(merchant.getMerchantCode());
        body.setSubject(merchant.getMerchantName()+"  消费"+totalAmount/100.00+"元");
        Order order=new Order();
        order.setMerchantCode(body.getMerchantCode());
        order.setTotalAmount(totalAmount);
        order.setStatus(Order.Status.PADDING.getCode());
        order.setOriReqMsgId(CodeGenerator.getUUID32());
        order.setSubject(body.getSubject());
        insert(order);
        String qrCode= MSSMPayService.getMSSMPayPlatformInstance().paySM(body,order.getOriReqMsgId()).getQrCode();
        order.setQrCode(qrCode);
        update(order);
        return qrCode;
    }

    public void update(Order order) {
        orderDao.update(order);
    }

    public List<Order> query(SearchObject searchObject) {
//        return orderDao.search(searchObject);
        return orderDao.queryByNutz();
    }

    public int queryCount(SearchObject searchObject) {
        return orderDao.searchCount(searchObject);
    }

    public void insert(MSNotify notify){
        notify.setHandled("0");
        orderDao.insert(notify);
    }

    public void handelNotify(MSNotify notify){
        Order order=orderDao.getById(notify.getReqMsgId());
        if(order==null){
            notify.setExtend1("本地订单不存在。");
        }else {
            if(Order.Status.PADDING.getCode().equals(order.getStatus())) {
                if (MSNotify.ClearOrCancel.CANCEL.getCode().equals(notify.getIsClearOrCancel())) {
                    order.setStatus(Order.Status.CANCEL.getCode());
                } else if (MSNotify.ClearOrCancel.CLEAR.getCode().equals(notify.getIsClearOrCancel())) {
                    order.setStatus(Order.Status.SUCCESS.getCode());
                }
                update(order);
            }else{
                LoggerFactory.getLogger().error("订单【"+notify.getReqMsgId()+"】已被处理。");
            }
        }
        notify.setHandled("1");
        update(notify);
    }

    public void update(MSNotify notify){
        orderDao.update(notify);
    }
    public MSNotify getNotify(String id){
        return orderDao.getNotify(id);
    }
}
